package com.kittlen.managementSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.kittlen.managementSystem.entity.*;
import com.kittlen.managementSystem.service.IChoiceService;
import com.kittlen.managementSystem.service.ICoachService;
import com.kittlen.managementSystem.utils.IdWorker;
import com.kittlen.managementSystem.utils.ObjectUtil;
import com.kittlen.managementSystem.utils.TimeUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学员选择表 前端控制器
 * </p>
 *
 * @author kittlen
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/choice")
public class ChoiceController {

    @Resource
    IChoiceService iChoiceService;

    @Resource
    ICoachService iCoachService;

    IdWorker idWorker=new IdWorker(1,1,1);

    /**
     * 记录查询
     *
     * @param choice
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("selects")
    public Result selects(Choice choice, @RequestParam(required = false, defaultValue = "0") int page,
                          @RequestParam(required = false, defaultValue = "0") int limit,
                          @RequestParam(required = false, defaultValue = "") String startTime,
                          @RequestParam(required = false, defaultValue = "") String endTime) {
        Result result = new Result();
        QueryWrapper<Choice> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNoEmpty(choice.getChoiceId())) {
            queryWrapper.like("choice_id", choice.getChoiceId());
        }
        if (ObjectUtil.isNoEmpty(choice.getStage())) {
            queryWrapper.like("stage", choice.getStage());
        }
        if (ObjectUtil.isNoEmpty(choice.getStageEnd())) {
            queryWrapper.like("stage_end", choice.getStageEnd());
        }
        if (ObjectUtil.isNoEmpty(choice.getStudent())) {
            if (ObjectUtil.isNoEmpty(choice.getStudent().getStudentId())) {
                queryWrapper.like("student_id", choice.getStudent().getStudentId());
            }
        }
        if (ObjectUtil.isNoEmpty(choice.getCoach())) {
            if (ObjectUtil.isNoEmpty(choice.getCoach().getCoachId())) {
                queryWrapper.like("co.coach_id", choice.getCoach().getCoachId());
            }
        }
        if (ObjectUtil.isNoEmpty(startTime)) {
            queryWrapper.ge("create_date", startTime);
        }
        if (ObjectUtil.isNoEmpty(endTime)) {
            endTime = TimeUtil.getSpecifiedDayAfter(endTime);
            queryWrapper.le("create_date", startTime);
        }
        if (page!=-1) {
            MySqlLimit mySqlLimit=new MySqlLimit(page,limit);
            queryWrapper.last("LIMIT "+mySqlLimit.getOffset()+","+mySqlLimit.getRows());
        }
        List<Choice> list = iChoiceService.selects(queryWrapper);
        result.setData(list);
        queryWrapper.last("");
        result.setCount(iChoiceService.count(queryWrapper));
        return result;
    }

    /**
     * 记录添加
     *
     * @param coachId
     * @return
     */
    @RequestMapping("/insert")
    public Result insert(String  coachId, HttpServletRequest request) throws Exception {
        Result result = new Result();
        Choice choice=new Choice();
        Student student=null;
        try {
            student= (Student) SessionController.getSession(request,"user");
            if(student.getStudyType()!=-1){
                result.setCode(result.RESULT_ERROR);
                result.setMsg("抱歉,您还未结束当前学习阶段,无法进行教练的选取");
                return result;
            }
            choice.setStudent(student);
        }catch (ClassCastException e){
            result.setCode(result.RESULT_ERROR);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        Coach coach=iCoachService.getById(coachId);
        if(ObjectUtil.isEmpty(coach)){
            result.setCode(result.RESULT_ERROR);
            result.setMsg("选取的教练信息异常,请重新选择");
            return result;
        }
        choice.setStage(student.getStudyStatus());//当前阶段
        choice.setStageEnd(false);
        choice.setCoach(coach);
        choice.setChoiceId(idWorker.nextId()+"");
        if (iChoiceService.insert(choice)) {
            student.setStudyType(0);
            request.getSession().setAttribute("user",student);
            result.setData(student);
            result.setMsg("教练选取成功");
        } else {
            result.setCode(result.RESULT_ERROR);
            result.setMsg("选择该教练失败");
        }
        return result;
    }

    /**
     * 修改学员的当前学习阶段
     * @param choicesStr 学员的选择json
     * @param studyType 类型:0学习中,1学习结束
     * @return
     * @throws Exception
     */
    @RequestMapping("/updates")
    public Result updates(String choicesStr,int studyType,String coachId) throws Exception {
        Result result=new Result();
        Choice.Choices choices = new Gson().fromJson(choicesStr,Choice.Choices.class);
        if(!iChoiceService.updates(choices,studyType,coachId)){
            result.setCode(result.RESULT_ERROR);
        }
        return result;
    }

    /**
     * 记录修改
     *
     * @param choice
     * @return
     */
    @RequestMapping("/update")
    public Result update(Choice choice) {
        Result result = new Result();
        if (iChoiceService.updateById(choice)) {
            result.setData(choice);
            result.setMsg("记录信息修改成功");
        } else {
            result.setCode(result.RESULT_ERROR);
            result.setMsg("记录信息修改失败");
        }
        return result;
    }

    /**
     * 记录删除
     *
     * @param choice
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Choice choice) {
        Result result = new Result();
        if (iChoiceService.removeById(choice)) {
            result.setMsg("删除成功");
        } else {
            result.setMsg("删除失败");
            result.setCode(result.RESULT_ERROR);
        }
        return result;
    }

}
